#include "Player.h"
#include "2d/CCActionTween.h"
#include "../aninode/FlowWord.h"

Player::Player() {
	m_isJumping = false;
}

Player::~Player() {
}

bool Player::init() {
	m_iHP = 1000;
	return true;
}

void Player::jump() {
	if (getSprite() == NULL) {
		return;
	}

	if (m_isJumping) {
		return;
	}

	m_isJumping = true;

	auto jump = JumpBy::create(2.0f, Point(0, 0), 250, 1);


	auto callFunc = CallFunc::create([&]() {
		m_isJumping = false;
	});

	auto jumpActions = Sequence::create(jump, callFunc, NULL);

	this->runAction(jumpActions);

	//auto scaleB = ActionTween::create(2.0f, "scale", 1, 3);
	//this->runAction(scaleB);
}

void Player::updateTweenAction(float value, const std::string& key) {
	cocos2d::log("value-> %f, key %s", value, key.c_str());
	this->setScale(value);

	if (value == 3.0f) {
		m_isJumping = false;
	}
}

void Player::hit()
{
	if (getSprite() != NULL) {

		FlowWord* flowWord = FlowWord::create();
		addChild(flowWord);

		flowWord->showWord("-15", getSprite()->getPosition());

		m_iHP -= 15;
		if (m_iHP < 0) {
			m_iHP = 0;
		}

		auto backMove = MoveBy::create(0.1f, Point(-20, 0));
		auto forwardMove = MoveBy::create(0.1f, Point(20, 0));
		auto backRotate = RotateBy::create(0.1, -5, 0);
		auto forwardRotate = RotateBy::create(0.1f, 5, 0);

		auto backActions = Spawn::create(backMove, backRotate, NULL);
		auto forwardActions = Spawn::create(forwardMove, forwardRotate, NULL);

		auto actions = Sequence::create(backActions, forwardActions, NULL);

		stopAllActions();

		resetData();

		runAction(actions);

	}
}

int Player::getHP()
{
	return m_iHP;
}

void Player::resetData() {
	if (m_isJumping) {
		m_isJumping = false;
	}
	setPosition(Point(200, 80));
	setScale(1.0f);
	setRotation(0);
}

